package com.naiterui.ehp.bs.emr.controller.doctor;

import java.util.List;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IDrCaseService;
import com.naiterui.ehp.bs.emr.service.IPatientCaseService;
import com.naiterui.ehp.bs.emr.vo.DrRecordVO;
import com.naiterui.ehp.bs.emr.vo.PatientOrDoctorCaseVO;
import com.naiterui.ehp.bs.emr.vo.RecordTemplateVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 诊疗记录
 *
 * @author lichaopi
 * @since 1.0.0
 */
@Validated
@RestController
@RequestMapping("emr/record")
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.MEDICAL_RECORD)
public class EmrMedicalRecordController {

    public static final Logger LOGGER = LoggerFactory.getLogger(EmrMedicalRecordController.class);

    @Autowired
    private IDrCaseService drCaseService;
    @Autowired
    private IPatientCaseService patientCaseService;

    /**
     * 病历记录分页
     * @param doctorId
     * @param patientId
     * @param pageVO
     * @param relation
     * @param inquirerId  就诊人为空时，根据患者查询
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "诊疗记录列表", notes = "诊疗记录列表", tags = {SwaggerTags.APP_DOCTOR_INQUIRER})
    @ApiImplicitParams({ @ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "relation", value = "筛选条件(0,1,2)", required = true, dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "inquirerId", value = "就诊人id", required = false, dataType = "long", paramType = "query") })
    @PostMapping("list")
    public ResponseVO<PageVO<DrRecordVO>> getRecordPage(@NotNull(message = "医生ID不能为null") Long doctorId,
                                                        @NotNull(message = "患者ID不能为null") Long patientId,
                                                        PageParamVO pageVO,
                                                        Integer relation,
                                                        Long inquirerId) throws BusinessException {
        LOGGER.info("病历列表请求参数:doctorId:{},patientId:{},relation:{}", doctorId, patientId, relation);

        PageVO<DrRecordVO> page = drCaseService.getMedicalRecordList(doctorId, patientId, pageVO.getPage(), pageVO.getNum(), relation,inquirerId);

        return PublicService.returnResponseVO(page);
    }

    /**
     * 查询患者病历祥情
     * DrRecordController.getMedicalRecord()
     *
     * @param recoreId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月29日
     * @since 1.0.0
     */
    @ApiOperation(value = "查询患者病历祥情（原名称：患者最近病历祥情,因为2。19需要这个名称）", notes = "查询患者病历祥情（原名称：患者最近病历祥情,因为2。19需要这个名称）", tags = {SwaggerTags.APP_DOCTOR_INQUIRER})
    @ApiImplicitParams({ @ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "recoreId", value = "病历id", required = true, dataType = "String", paramType = "query") })
    @PostMapping("medicalRecord")
    public ResponseVO<PatientOrDoctorCaseVO> getMedicalRecord(@NotEmpty(message = "病历ID不能为null") String recoreId) throws BusinessException {
        LOGGER.info("患者病历祥情请求参数:recoreId:{}", recoreId);
        PatientOrDoctorCaseVO vo = drCaseService.getPtOrDrMedicalRecord(recoreId);
        return PublicService.returnResponseVO(vo);
    }

    /**
     * 患者最近病历祥情
     * MedicalRecordController.getLatelyMedicalRecord()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月25日
     * @since 1.0.0
     */
    @ApiOperation(value = "患者最近病历祥情(2.19新加)", notes = "患者最近病历祥情(2.19新加)")
    @ApiImplicitParams({ @ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "Long", paramType = "query") })
    @PostMapping("latelyMedicalRecord")
    public ResponseVO<PatientOrDoctorCaseVO> getLatelyMedicalRecord(@NotNull(message = "医生ID不能为null") Long doctorId,
                                                                    @NotNull(message = "患者ID不能为null") Long patientId) throws BusinessException {
        LOGGER.info("查询最近病历,doctorId:{},patientId:{}", doctorId, patientId);
        PatientOrDoctorCaseVO vo = drCaseService.getLateRecord(doctorId, patientId);
        return PublicService.returnResponseVO(vo);
    }


    /**
     * 病历模板列表
     * MedicalRecordController.getRecordTempList()
     *
     * @return
     *
     * @Author yingsu
     * @Date 2018年11月14日
     * @since 3.5.0
     */
    @ApiOperation(value = "病历模板列表", notes = "病历模板列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "add") })
    @PostMapping("template")
    public ResponseVO<List<RecordTemplateVO>> getRecordTempList() throws BusinessException {
        LOGGER.info("获取病历模板列表");
        List<RecordTemplateVO> list = drCaseService.getTemplateList();
        return PublicService.returnResponseVO(list);
    }

    @ApiOperation(value = "获取医生是否给指定患者填写过病历", notes = "获取医生是否给指定患者填写过病历")
    @PostMapping("patient/count")
    public ResponseVO<Integer> getMedicalRecordCount(Long doctorId, Long patientId) throws BusinessException {
        LOGGER.info("获取医生是否给指定患者填写过病历,doctorId={},patientId={}", doctorId, patientId);
        boolean exist = drCaseService.getCaseExist(patientId, doctorId);
        //TODO 2019/10/14 4:24 此处固定返回有，不阻塞消息发送逻辑
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, 1);
        //return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, exist ? 1 : 0);
    }

}
